package com.zx.oa.controller;

import com.alibaba.excel.EasyExcel;
import com.zx.oa.dao.ClassInfo.ClassInfoDao;
import com.zx.oa.dao.ClassInfo.ClassInfoDaoImpl;
import com.zx.oa.dao.census.CensusDao;
import com.zx.oa.dao.census.CensusDaolpml;
import com.zx.oa.entity.*;
import com.zx.oa.service.ClassInfo.ClassInfoService;
import com.zx.oa.service.ClassInfo.ClassInfoServiceImpl;
import com.zx.oa.utils.PageSupport;
import com.zx.oa.utils.excel.JobInfo;
import com.zx.oa.utils.excel.StudentExcel;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@WebServlet("/workResultInfo.do")
public class WorkResultInfo extends HttpServlet {

    ClassInfoService classInfoService=new ClassInfoServiceImpl();
    List<String> list=Arrays.asList("完成", "未完成", "未全部完成", "请假未完成");
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String caozou=request.getParameter("param");
        if ("cha".equals(caozou)){
            chaServelt(request,response);
        }else if ("delete".equals(caozou)){
            deleteServlet(request,response);
        }else if ("modifysave".equals(caozou)){
            modifysaveServlet(request,response);
        } else if ("execl".equals(caozou)) {
            execlServlet(request,response);
        } else if ("execlMonth".equals(caozou)) {
            execlMonth(request,response);
        }
    }

    private void execlMonth(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String major = request.getParameter("majorJsp");
        String classname = request.getParameter("classnameJsp");
        String month = request.getParameter("month");

        List<CheckJob> workreportInfos= classInfoService.getClassInfo(major,classname,month);

        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取到时间，以时间来定义下载Excel的名称
        String downLoadTime=sf.format(new Date());

        //设置Excel响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //下载的Excel文件名称
        String fileName="CheckJobMonth"+downLoadTime+".xlsx";
        response.setHeader("Content-disposition","allachment;filename="+fileName);
        EasyExcel.write(response.getOutputStream(),
                JobInfo.class).sheet("月份的作业完成情况").doWrite(workreportInfos);
    }

    private void execlServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取到时间，以时间来定义下载Excel的名称
        String downLoadTime=sf.format(new Date());

        List<CheckJob> workreportInfos= (List<CheckJob>) request.getSession().getAttribute("workreportInfos");

        //设置Excel响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //下载的Excel文件名称
        String fileName="CheckJob"+downLoadTime+".xlsx";
        response.setHeader("Content-disposition","allachment;filename="+fileName);
        EasyExcel.write(response.getOutputStream(),
                JobInfo.class).sheet("作业完成情况").doWrite(workreportInfos);
    }

    private void modifysaveServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        String cktime = request.getParameter("zuoyeDate");
        String ckstatu = request.getParameter("result");
        boolean flag=true;
        if (null == cktime || "".equals(cktime)) {
            String ckstatu1=request.getParameter("dateNull");
            flag=classInfoService.updateJob(id,ckstatu1,ckstatu);
        }else {
            flag=classInfoService.updateJob(id,cktime,ckstatu);
        }

            response.sendRedirect("workResultInfo.do?param=cha");

    }

    private void deleteServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag = classInfoService.deleteCheckJobInfo(id);
        if (flag) {
            response.sendRedirect("workResultInfo.do?param=cha");
        }
    }

    private void chaServelt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String major=null;//查询的专业的名字
        String className=null;//查询的班级的名字
        String majorJsp=request.getParameter("majorJsp" );
        String classNameJsp=request.getParameter("classnameJsp" );
        if(null!=majorJsp&&!"".equals(majorJsp)){
            major=majorJsp;
        }
        if(null!=classNameJsp&&!"".equals(classNameJsp)){
            className=classNameJsp;
        }

        String studentName=null;//查询的学生的姓名
        String date=null;//查询的时间
        String result=null;
        String studentName1=request.getParameter("StudentName" );
        String date1=request.getParameter("date" );
        String result1=request.getParameter("jobResult" );

        if(null!=studentName1&&!"".equals(studentName1)){
            studentName=studentName1;
        }
        if(null!=date1&&!"".equals(date1)){
            date=date1;
        }
        if(null!=result1&&!"".equals(result1)){
            result=result1;
        }


        String pageIndex= request.getParameter("pageIndex");
        if(pageIndex==null){
            //第一次访问查询的servlet的时候没有pageIndex，赋默认值1
            pageIndex="1";
        }
        int currentPageNo=1;//当前页号
        int pageSize= PageSupport.pageSizeVal;//页面尺寸复制
        currentPageNo=Integer.parseInt(pageIndex);

        CensusDao censusDao=new CensusDaolpml();

        ClassInfoDao classInfoDao=new ClassInfoDaoImpl();
        int totalCount= classInfoDao.getClassTotalCount(major,className,studentName,date,result);

        //存储页面信息
        PageSupport pages=new PageSupport();
        //设置页面尺寸
        pages.setPageSize(pageSize);
        //设置页面总记录数（注意此方法计算得到总页面数量）
        pages.setTotalCount(totalCount);
        //获取到总的页数
        int totalPageCount=pages.getTotalPageCount();
        if(currentPageNo<1){
            currentPageNo=1;
        }else if(currentPageNo>totalPageCount){
            currentPageNo=totalPageCount;
        }
        //将当前页号存储到pages对象
        pages.setCurrentPageNo(currentPageNo);

        List<CheckJob> workreportInfos= classInfoService.getClassInfo(major,className,studentName,date,result,currentPageNo,pageSize);


        List<Major> majorList=classInfoService.getMajorAll();
        request.setAttribute("majorList",majorList);

        //获取到全部的班级
        List<ClassInfo> classInfoList=classInfoService.getClassAll(major);
        request.setAttribute("classInfoList",classInfoList);

        List<CheckJob> jobAll= classInfoService.getJob();
        request.setAttribute("jobAll",jobAll);

        request.getSession().setAttribute("workreportInfos",workreportInfos);
        request.setAttribute("pageInfo",pages);

        request.getSession().setAttribute("statuList",list);
        request.setAttribute("studentName",studentName);
         request.setAttribute("date",date);
         request.setAttribute("classNameJsp",classNameJsp);
         request.setAttribute("result",result);
        request.getRequestDispatcher("report/reportWorkResultInfo.jsp").forward(request,response);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
